from selenium.webdriver.common.by import By

from element_base import TargetBase, CommandBase
from tools import DriverTools


# 对象库层
class Target(TargetBase):
    def __init__(self):
        super().__init__()
        self.driver = DriverTools().OpenDriver()
        self.mobile = By.ID, "com.bjcsxq.chat.carfriend:id/login_phone_et"
        self.password = By.ID, "com.bjcsxq.chat.carfriend:id/login_pwd_et"
        self.check_btn = By.ID, "com.bjcsxq.chat.carfriend:id/check"
        self.login_btn = By.ID, "com.bjcsxq.chat.carfriend:id/login_btn"

    def moblie_element(self):
        return TargetBase().target_element(self.mobile)

    def password_element(self):
        return TargetBase().target_element(self.password)

    def check_button(self):
        return TargetBase().target_element(self.check_btn)

    def login_button(self):
        return TargetBase().target_element(self.login_btn)


# 操作层
class Command(CommandBase):
    def __init__(self):
        self.target = Target()

    def input_mobile(self, mobile):
        CommandBase().clear_text(self.target.moblie_element(), mobile)

    def input_password(self, password):
        CommandBase().clear_text(self.target.password_element(), password)

    def click_check(self):
        self.element = self.target.check_button()
        if self.element.get_attribute("checked") == "false":
            self.element.click()

    def click_login(self):
        self.target.login_button().click()


# 业务层
class Login():
    def __init__(self):
        self.command = Command()

    def execute(self, mobile, password):
        self.command.input_mobile(mobile)
        self.command.input_password(password)
        self.command.click_check()
        self.command.click_login()
